<template>
  <view class="driving-detail-container" v-if="!isLoading">
    <view v-if="school">
    <u-navbar title="驾校详情" :autoBack="false"></u-navbar>
    
    <image class="school-img" :src="school.image" mode="aspectFill"></image>
    <view class="school-info">
      <view class="school-title-row">
        <view class="school-title">{{ school.name }}</view>
        <view class="school-tags">
          <text class="school-tag">热门</text>
          <text class="school-tag">推荐</text>
          <text class="school-tag">认证</text>
        </view>
      </view>
      <view class="school-address">{{ school.address }}</view>
      <view class="school-types">
        <text class="type-label">可报车型：</text>
        <text class="type-tag" v-for="(tag, tagIndex) in school.tags" :key="tagIndex">{{ tag }}</text>
      </view>
    </view>

    <!-- 驾校简介模块 -->
    <view class="section-block">
      <view class="section-title-row">
        <view class="section-title">驾校简介</view>
        <view class="section-link">查看简介</view>
      </view>
      <view class="section-content">{{ school.intro || '本驾校拥有多年教学经验，师资力量雄厚，教学设施先进，服务优质，致力于为学员提供高效、舒适的学车体验。' }}</view>
    </view>

    <!-- 班型模块 -->
    <view class="section-block">
      <view class="section-title-row">
        <view class="section-title">班型介绍</view>
        <view class="section-link" @click="goToClassList">查看简介</view>
      </view>
      <view class="section-content">
        <view v-if="!school.classTypes || school.classTypes.length === 0" class="loading-text">暂无班型信息</view>
        <view v-else>
          <view class="class-type-row" v-for="(classType, index) in school.classTypes" :key="index">
            <view class="class-type-img">
              <image :src="classType.image" mode="aspectFit"></image>
            </view>
            <view class="class-type-info">
              <view class="class-type-title">{{ classType.name }}</view>
              <view class="class-type-desc">{{ classType.description }}</view>
              <view class="class-type-features">
                <text class="feature-tag" v-for="(feature, featureIndex) in classType.features" :key="featureIndex">{{ feature }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 教练团队模块 -->
    <view class="section-block">
      <view class="section-title-row">
        <view class="section-title">教练团队</view>
        <view class="section-link">查看更多</view>
      </view>
      <view class="section-content">
        <view class="coach-grid">
          <view class="coach-card" v-for="(coach, index) in school.coaches" :key="index">
            <image class="coach-img" :src="coach.img" mode="aspectFill"></image>
            <view class="coach-name">{{ coach.name }}</view>
            <view class="coach-intro">{{ coach.intro }}</view>
          </view>
        </view>
      </view>
    </view>

      <!-- 底部立即报名按钮 -->
      <view class="driving-bottom-bar">
        <view class="driving-signup-btn" @click="showSignupDialog" @tap="showSignupDialog">立即报名</view>
      </view>
      
      <!-- 报名对话框 -->
      <view class="signup-dialog" v-if="showDialog" @click="hideSignupDialog">
        <view class="dialog-content" @click.stop>
          <view class="dialog-header">
            <text class="dialog-title">立即报名</text>
            <text class="dialog-close" @click="hideSignupDialog">×</text>
          </view>
          
          <view class="dialog-body">
            <!-- 驾校信息 -->
            <view class="school-info" v-if="school">
              <view class="school-name">{{ school.name }}</view>
              <view class="school-address">{{ school.address }}</view>
              <view class="school-tags">
                <text class="tag" v-for="(tag, index) in school.tags" :key="index">{{ tag }}</text>
              </view>
            </view>
            
            <!-- 表单 -->
            <view class="form-section">
              <view class="form-item">
                <text class="form-label">姓名</text>
                <input 
                  class="form-input" 
                  v-model="formData.name" 
                  placeholder="请输入您的姓名"
                  maxlength="20"
                />
              </view>
              
              <view class="form-item">
                <text class="form-label">手机号</text>
                <input 
                  class="form-input" 
                  v-model="formData.phone" 
                  placeholder="请输入您的手机号"
                  type="number"
                  maxlength="11"
                />
              </view>
            </view>
            
            <!-- 提交按钮 -->
            <view class="submit-btn" @click="submitSignup">
              <text>确认报名</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view v-else class="empty-detail">
      <text>未找到该驾校</text>
    </view>
  </view>
  <view v-else class="loading-container">
    <text>加载中...</text>
  </view>
</template>

<script>
import { getSchoolDetail, applySchool } from '@/src/api/driver/index.js'

export default {
  data() {
    return {
      schoolId: null,
      school: null,
      isLoading: true,
      // 报名相关
      showDialog: false,
      formData: {
        name: '',
        phone: ''
      }
    }
  },
    onLoad(options) {
    this.schoolId = options && options.id ? parseInt(options.id) : 1;
    this.loadSchoolDetail();
  },
  methods: {
    async loadSchoolDetail() {
      try {
        const response = await getSchoolDetail(this.schoolId);
        this.isLoading = false;
        
        if (response.code === 200 && response.data) {
          this.school = response.data;
          console.log('驾校数据加载成功:', this.school);
        } else {
          console.log('驾校数据加载失败:', response);
          uni.showToast({
            title: '获取驾校信息失败',
            icon: 'none'
          });
        }
      } catch (error) {
        this.isLoading = false;
        console.log('加载驾校数据出错:', error);
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    },
    
    async applySchool() {
      try {
        const response = await applySchool(1, '阳光驾校');
        if (response.code === 200) {
          uni.showToast({
            title: response.message,
            icon: 'success'
          });
        } else {
          uni.showToast({
            title: response.message || '报名失败',
            icon: 'none'
          });
        }
      } catch (error) {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    },
    
    goToClassList() {
      uni.navigateTo({
        url: '/driver/class/index'
      });
    },
    
    showSignupDialog() {
      this.showDialog = true;
    },
    
    hideSignupDialog() {
      this.showDialog = false;
    },
    
    submitSignup() {
      if (!this.formData.name.trim()) {
        uni.showToast({
          title: '请输入姓名',
          icon: 'none'
        });
        return;
      }
      
      if (!this.formData.phone.trim()) {
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        });
        return;
      }
      
      if (!/^1[3-9]\d{9}$/.test(this.formData.phone)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
        return;
      }
      
      // 提交报名
      uni.showLoading({
        title: '提交中...'
      });
      
      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({
          title: '报名成功',
          icon: 'success'
        });
        this.hideSignupDialog();
        this.formData = { name: '', phone: '' };
      }, 1500);
    }
  }
}
</script>

<style scoped lang="scss">
.driving-detail-container {
  background: #f5f5f5;
  min-height: 100vh;
  padding: 0 0 160rpx 0;
  padding-top: 180rpx;
}

.school-img {
  width: 100%;
  height: 400rpx;
  object-fit: cover;
  border-radius: 0;
  margin-top: 0;
}

.school-info {
  background: #fff;
  border-radius: 0;
  margin: 0;
  padding: 40rpx 20rpx 20rpx 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.06);
  position: relative;
  top: 0;
  margin-bottom: 0;
}

.school-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #222;
  margin-bottom: 16rpx;
}

.school-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.school-tags {
  display: flex;
  gap: 12rpx;
}

.school-tag {
  font-size: 20rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 8rpx;
  padding: 4rpx 14rpx;
  font-weight: 500;
}

.school-address {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.school-types {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.type-label {
  font-size: 24rpx;
  color: #888;
}

.type-tag {
  font-size: 22rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 8rpx;
  padding: 4rpx 16rpx;
  margin-right: 8rpx;
}

.empty-detail {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400rpx;
  color: #999;
  font-size: 28rpx;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  color: #999;
  font-size: 28rpx;
}

.section-block {
  background: #fff;
  border-radius: 0;
  margin: 0;
  padding: 30rpx 20rpx 20rpx 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.04);
  border-bottom: 2rpx solid #f0f0f0;
  margin-top: 0;
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #1296db;
  margin-bottom: 16rpx;
}

.section-content {
  font-size: 24rpx;
  color: #444;
  line-height: 1.7;
}

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.section-link {
  font-size: 22rpx;
  color: #1296db;
  margin-right: 20rpx;
}

.class-type-row {
  display: flex;
  align-items: center;
  margin-bottom: 16rpx;
}

.class-type-img {
  width: 120rpx;
  height: 160rpx;
  border-radius: 8rpx;
  margin-right: 18rpx;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  
  image {
    width: 100%;
    height: 100%;
    border-radius: 8rpx;
  }
}

.class-type-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.class-type-title {
  font-size: 24rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 4rpx;
}

.class-type-desc {
  font-size: 22rpx;
  color: #888;
  margin-bottom: 8rpx;
}

.class-type-features {
  display: flex;
  flex-wrap: wrap;
  gap: 8rpx;
  margin-bottom: 8rpx;
}

.feature-tag {
  font-size: 20rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 6rpx;
  padding: 2rpx 8rpx;
}

.class-type-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.class-type-meta .price {
  font-size: 24rpx;
  font-weight: bold;
  color: #ff6b35;
}

.class-type-meta .duration {
  font-size: 20rpx;
  color: #999;
}

.coach-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20rpx;
  margin-top: 10rpx;
}

.coach-card {
  background: #f7f7f7;
  border-radius: 12rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx 0;
}

.coach-img {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 10rpx;
}

.coach-name {
  font-size: 22rpx;
  color: #222;
  font-weight: bold;
  margin-bottom: 4rpx;
  text-align: center;
}

.coach-intro {
  font-size: 20rpx;
  color: #888;
  text-align: center;
}

.driving-bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.04);
  padding: 20rpx 40rpx 60rpx 40rpx;
  z-index: 1000;
  display: flex;
  justify-content: center;
}

.driving-signup-btn {
  width: 90%;
  border-radius: 10rpx;
  background: #1296db;
  color: #fff;
  font-size: 28rpx;
  font-weight: bold;
  padding: 20rpx 0;
  text-align: center;
  margin: 0 auto;
}

/* 对话框 */
.signup-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: slideUp 0.3s ease-out;
}

.dialog-content {
  background: #fff;
  width: 100%;
  border-radius: 24rpx 24rpx 0 0;
  padding: 40rpx 30rpx;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
  z-index: 10000;
  box-shadow: 0 -4rpx 20rpx rgba(0,0,0,0.1);
  min-height: 400rpx;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30rpx;
  padding-bottom: 20rpx;
  border-bottom: 2rpx solid #f0f0f0;
}

.dialog-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.dialog-close {
  font-size: 40rpx;
  color: #999;
  padding: 10rpx;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dialog-body {
  padding-bottom: 40rpx;
}

/* 驾校信息 */
.school-info {
  background: #f8f9fa;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 30rpx;
}

.school-name {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.school-address {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 12rpx;
}

.school-tags {
  display: flex;
  gap: 8rpx;
}

.tag {
  font-size: 20rpx;
  color: #1296db;
  background: #e6f3ff;
  border-radius: 6rpx;
  padding: 4rpx 12rpx;
}

/* 表单 */
.form-section {
  margin-bottom: 30rpx;
}

.form-item {
  margin-bottom: 20rpx;
}

.form-label {
  display: block;
  font-size: 26rpx;
  color: #333;
  margin-bottom: 8rpx;
  font-weight: 500;
}

.form-input {
  width: 100%;
  height: 80rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 26rpx;
  color: #333;
  background: #fff;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #1296db;
}

/* 提交按钮 */
.submit-btn {
  background: linear-gradient(135deg, #1296db, #0d8bc7);
  color: #fff;
  text-align: center;
  padding: 24rpx;
  border-radius: 12rpx;
  font-size: 28rpx;
  font-weight: bold;
  margin-top: 20rpx;
}

/* 动画 */
@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}
</style> 